#define  _CRT_SECURE_NO_WARNINGS
// class Solution {
// public:
//     int maxTurbulenceSize(vector<int>& arr) {
//         int n = arr.size();
//         vector<vector<int>> dp(n, vector<int>(2, 1));
//         int ret = 1;
//         for(int i = 1; i < n; i++)
//         {
//             if(arr[i - 1] < arr[i])
//                 ret = max(ret, dp[i][0] = dp[i - 1][1] + 1);
//             else if(arr[i - 1] > arr[i])
//                 ret = max(ret, dp[i][1] = dp[i - 1][0] + 1);
//         }
//         return ret;
//     }
// };
class Solution {
public:
    int maxTurbulenceSize(vector<int>& arr) {
        int n = arr.size();
        int up, down;
        int ret = up = down = 1;
        for (int i = 1; i < n; i++)
        {
            if (arr[i - 1] < arr[i])
            {
                ret = max(ret, up = down + 1);
                down = 1;
            }
            else if (arr[i - 1] > arr[i])
            {
                ret = max(ret, down = up + 1);
                up = 1;
            }
            else
            {
                up = down = 1;
            }
        }
        return ret;
    }
};